﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using SharedGenomics.Annotations;
using SharedGenomics.Workbench.Annotations;
using SharedGenomics.Workbench.Tests.TestObjects;

using Rhino.Mocks;
using Rhino.Mocks.Constraints;

namespace SharedGenomics.Workbench.Tests.Annotations
{
    [TestClass]
    public class LookupAnnotationPreRequestStageTests
    {
        private MockRepository mocks;
        private IAnnotationRepositoryMediator annotations;
        private LookupAnnotationRequestStage requestStage;

        [TestInitialize]
        public void Setup()
        {
            this.mocks = new MockRepository();
            this.annotations = this.mocks.Stub<IAnnotationRepositoryMediator>();

            this.requestStage = new LookupAnnotationRequestStage(this.annotations);
        }

        [TestMethod]
        public void ShouldNotSetAnnotation()
        {
            using (this.mocks.Record())
            {
                this.annotations.Stub(ap => ap.GetAnnotationForTarget(AnnotationKey.GoogleSearch, "rs001")).Return(null);
            }

            using (this.mocks.Playback())
            {
                var context = new AnnotationRequestContext(AnnotationKey.GoogleSearch, "rs001");
                var traversalStrategy = new TestAnnotationRequestPipelineTraversalStrategy();
                this.requestStage.Process(context, traversalStrategy);

                Assert.IsFalse(context.ContainsResultFor(AnnotationKey.GoogleSearch));
                Assert.IsTrue(traversalStrategy.MovedNext);
            }
        }

        [TestMethod]
        public void ShouldSetAnnotation()
        {
            using (this.mocks.Record())
            {
                GoogleSearchAnnotation a = new GoogleSearchAnnotation("rs001");
                this.annotations.Stub(ap => ap.GetAnnotationForTarget(AnnotationKey.GoogleSearch, "rs001")).Return(a);
            }

            using (this.mocks.Playback())
            {
                var context = new AnnotationRequestContext(AnnotationKey.GoogleSearch, "rs001");
                var traversalStrategy = new TestAnnotationRequestPipelineTraversalStrategy();
                this.requestStage.Process(context, traversalStrategy);

                Assert.IsTrue(context.ContainsResultFor(AnnotationKey.GoogleSearch));
                Assert.IsTrue(traversalStrategy.MovedNext);
            }
        }
    }
}
